Page({
  data: {
    dialogVisible: false,
    houseList: []
  },
  id: '',
  // 获取数据渲染
  async onLoad () {
    const res = await wx.http({ url: '/room' })
    console.log(res)
    this.setData({ houseList: res.data })
  },
  // 显示右划删除
  swipeClose (ev) {
    console.log(ev)
    const { position, instance } = ev.detail

    if (position === 'right') {
      // 储存id
      this.id = ev.mark.id
      // console.log(this.id)
      // 显示 Dialog 对话框
      this.setData({
        dialogVisible: true
      })

      // swiper-cell 滑块关闭
      instance.close()
    }
  },
  // 实现删除
  async dialogClose (e) {
    // console.log(e)
    // 判断点的是确定时
    if (e.detail === 'confirm') {
      // 删除数据
      await wx.http({ url: `/room/${this.id}`, method: 'DELETE' })

      const res = await wx.http({ url: '/room' })
      this.setData({ houseList: res.data })
    }
  },

  goDetail (e) {
    wx.navigateTo({
      url: '/house_pkg/pages/detail/index?houseId=' + e.mark.id
    })
  },

  addHouse () {
    wx.navigateTo({
      url: '/house_pkg/pages/locate/index'
    })
  }
})
